package cn.bdqn.filter;

import cn.bdqn.dao.sys.SysUserDao;
import cn.bdqn.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ProjectName: ycbdqn
 * @Package: cn.bdqn.filter
 * @Author: huat
 * @Date: 2020/3/19 12:01
 * @Version: 1.0
 */
@Component
public class LogingFilter implements HandlerInterceptor {
    @Autowired
    SysUserDao sysUserDao;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session=request.getSession();
        SysUser sysUser=(SysUser) session.getAttribute("user");
        String sessionId=sysUserDao.getSessionIdById(sysUser.getId());
        if(session.getId().equals(sessionId)){
            return true;
        }
        response.sendRedirect("/intoLogin");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
